package com.lc.qianrenshiye.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.GetCodePost;
import com.lc.qianrenshiye.conn.UpdatePasswordPost;
import com.lc.qianrenshiye.uitls.MyToast;
import com.lc.qianrenshiye.uitls.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMD5;

/**
 * Created by Administrator on 2016/11/16.
 */
public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener{


    @BoundView(R.id.change_comfirm_tv) private TextView change_comfirm_tv;
    @BoundView(R.id.change_get_code_tv) private TextView change_get_code_tv;

    @BoundView(R.id.change_username_et) private EditText change_username_et;
    @BoundView(R.id.change_password_code_et) private EditText change_password_code_et;
    @BoundView(R.id.change_comfirm_password_et) private EditText change_comfirm_password_et;
    @BoundView(R.id.change_new_password_et) private EditText change_new_password_et;

    String netCode;

    private UpdatePasswordPost updatePasswordPost = new UpdatePasswordPost(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            MyToast.showToast(toast, context);

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }
    });

    private GetCodePost getCodePost = new GetCodePost(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            netCode = s;
            MyToast.showToast(toast, context);

            countTime.start();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            netCode = "";

            MyToast.showToast(toast, context);

            change_get_code_tv.setEnabled(true);

        }
    });

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if(!isFinishing()) {

                change_get_code_tv.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if(!isFinishing()) {
                change_get_code_tv.setText("重新获取");
                change_get_code_tv.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        setBackTrue();

        setTitleName(getString(R.string.change_password_text));



        change_comfirm_tv.setOnClickListener(this);
        change_get_code_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.change_comfirm_tv:

                String userName = change_username_et.getText().toString().trim();
                String code = change_password_code_et.getText().toString().trim();

                if (TextUtils.isEmpty(userName)) {

                    MyToast.showToast(getString(R.string.please_fill_usename_text_hint), context);
                    return;

                } else if (!Validator.isMobile(userName)) {

                    MyToast.showToast(getString(R.string.please_fill_true_phone_text), context);
                    return;

                } else if (TextUtils.isEmpty(netCode)) {

                    MyToast.showToast(getString(R.string.please_click_get_code_text), context);
                    return;

                } else if (TextUtils.isEmpty(code)) {

                    MyToast.showToast(getString(R.string.please_fill_code_text), context);
                    return;

                } else if (!code.equals(netCode)) {

                    MyToast.showToast(getString(R.string.please_fill_true_code_text), context);
                    return;

                } else if (TextUtils.isEmpty(getNewPassword())) {

                    MyToast.showToast(getString(R.string.please_fill_new_password_hint), context);
                    return;

                } else if (TextUtils.isEmpty(getConfirmPassword())) {

                    MyToast.showToast(getString(R.string.please_fill_confirm_password_text), context);
                    return;

                } else if (!getConfirmPassword().equals(getNewPassword())) {

                    MyToast.showToast(getString(R.string.password_not_same_text), context);
                    return;

                }

                updatePasswordPost.code = code;
                updatePasswordPost.phone = userName;
                updatePasswordPost.newpass = UtilMD5.MD5Encode(UtilMD5.MD5Encode(getNewPassword(), "UTF-8"), "UTF-8");
                updatePasswordPost.execute(this);

                break;

            case R.id.change_get_code_tv:

                userName = change_username_et.getText().toString().trim();

                if (TextUtils.isEmpty(userName)) {

                    MyToast.showToast(getString(R.string.please_fill_usename_text_hint), context);
                    return;

                }
                else if (!Validator.isMobile(userName)) {

                    MyToast.showToast(getString(R.string.please_fill_true_phone_text), context);
                    return;

                }

                change_get_code_tv.setEnabled(false);

                getCodePost.phone = userName;
                getCodePost.type = "set";
                getCodePost.execute(this);

                break;
        }
    }

    private String getNewPassword () {

        return change_new_password_et.getText().toString();

    }

    private String getConfirmPassword () {

        return change_comfirm_password_et.getText().toString();
    }


}
